package com.atguigu.senior;

import com.atguigu.senior.dao.BankDao;
import com.atguigu.senior.dao.EmployeeDao;
import com.atguigu.senior.dao.impl.BankDaoImpl;
import com.atguigu.senior.dao.impl.EmployeeDaoImpl;
import com.atguigu.senior.pojo.Employee;
import com.atguigu.senior.utils.JDBCUtilV1;
import com.atguigu.senior.utils.JDBCUtilV2;
import org.junit.Test;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @author lhl
 * @date 2025/4/16 16:35
 */
public class JDBCUtilTest {


    @Test
    public void testGetConnection() {
        Connection connection = JDBCUtilV1.getConnection();

        System.out.println(connection);

        JDBCUtilV1.release(connection);
    }

    @Test
    public void testJDBCV2() {
//        Connection connection1 = JDBCUtilV1.getConnection();
//        Connection connection2 = JDBCUtilV1.getConnection();
//        Connection connection3 = JDBCUtilV1.getConnection();
//
//        System.out.println(connection1);
//        System.out.println(connection2);
//        System.out.println(connection3);

        Connection connection4 = JDBCUtilV2.getConnection();
        Connection connection5 = JDBCUtilV2.getConnection();
        Connection connection6 = JDBCUtilV2.getConnection();

        System.out.println(connection4);
        System.out.println(connection5);
        System.out.println(connection6);
    }


    @Test
    public void testEmployeeDaoSelectAll() {
        //1.创建Dao实现对象
        EmployeeDao employeeDao = new EmployeeDaoImpl();
        //查询所有列表
        List<Employee> employeeList = employeeDao.selectAll();
        for (Employee employee : employeeList) {
            System.out.println(employee);
        }
    }

    @Test
    public void testEmployeeDaoSelectByEmpId() {
        //1.创建Dao实现对象
        EmployeeDao employeeDao = new EmployeeDaoImpl();
        //查询指定条件的数据
        Employee employee = employeeDao.selectByEmpId(1);
        System.out.println(employee);


    }

    @Test
    public void testEmployeeDaoInsert() {
        //1.创建Dao实现对象
        EmployeeDao employeeDao = new EmployeeDaoImpl();
        //插入
        int count = employeeDao.insert(new Employee(null, "lhl", 235.23, 18));
        System.out.println(count);


    }

    @Test
    public void testEmployeeDaoUpdate() {
        //1.创建Dao实现对象
        EmployeeDao employeeDao = new EmployeeDaoImpl();
        //修改
        int count = employeeDao.update(new Employee(2120, "rose", 505.4, 100));
        System.out.println(count);

    }

    @Test
    public void testEmployeeDaoDelete() {
        //1.创建Dao实现对象
        EmployeeDao employeeDao = new EmployeeDaoImpl();
        //删除
        int count = employeeDao.delete(2120);
        System.out.println(count);
    }

    @Test
    public void testTransaction(){
        BankDao bankDao = new BankDaoImpl();
        Connection connection = null;
        try {
            //1.获取连接，将连接的事务改成手动提交
            connection = JDBCUtilV2.getConnection();
            connection.setAutoCommit(false);

            //2.操作减钱
            bankDao.subMoney(1,100);

            int i = 10 / 0;

            //3.操作加钱
            bankDao.addMoney(2,100);

            //4.前置dao操作没有异常，就提交事务
            connection.commit();
        } catch (Exception e) {
            try {
                connection.rollback();
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }finally {
            JDBCUtilV2.release();
        }
    }
}
